<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="btnaddBrand">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="应用名称" value="appName"></el-option>
              <el-option label="应用包名" value="appPackage"></el-option>
              <el-option label="归属" value="username"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="tableData" :key="key" border fit highlight-current-row ref="tableDataRef" max-height="535">
      <el-table-column v-if="colData[0].istrue" min-width="180" align="center" prop="username" label="归属"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="130" align="center" prop="batch" label="批次"></el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="180" align="center" prop="app_name" label="默认霸屏应用"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="160" align="center" prop="app_package" label="包名"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="140" align="center" prop="create_time" label="创建时间">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getqueryIdAppControl(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <!-- 添加dialog表单 -->
    <el-dialog :title="editSubjects ? '应用霸屏设置' : '编辑'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="35%" @close="addFormclose">
      <el-form :model="addForm" ref="ruleForm" :rules="rules" style="padding-top:0">
        <el-form-item label="批次:" label-width="80px" prop="batch">
          <el-select v-model="addForm.batch" placeholder="请选择批次" class="add_select" multiple filterable>
            <el-option v-for="item in lotoptions" :key="item.id" :label="item.batch" :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用:" label-width="80px" prop="appId">
          <el-select v-model="addForm.appId" placeholder="请选择应用" class="add_select">
            <el-option v-for="(item) in appOptions" :key="item.app_id" :label="item.app_name" :value="item.app_id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" @click="getaddAppControl('ruleForm')" size="mini" round>确 定</el-button>
        <el-button v-else type="primary" @click="gettimeeditAppControl('ruleForm')" size="mini" round>编辑</el-button>
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import deleteDialog from "@/components/deleteDialog";
import { timequeryAppControl, getAppName, addAppControl, queryIdAppControl, timeeditAppControl, deleteAppControl } from "@/api/deployment.js";
import { getBatchTwo } from "@/api/equipment.js";
import tableMini from '@/utils/tableMini.js'
export default {
  mixins: [tableMini],
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["归属", "批次", "默认霸屏应用", "包名", "创建时间"],
      //归属table
      tableData: [],
      select: "", //搜索条件
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), appName: undefined, appPackage: undefined, username: undefined },
      //添加按钮控制
      dialogFormVisible: false,
      //编辑按钮控制
      editSubjects: true,
      //浏览器专题id
      subjectId: 0,
      //家长SN码下拉选择
      lotoptions: [],
      appOptions: [],
      //添加表单
      addForm: { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), batch: "", appId: "" },
      rules: { batch: [{ required: true, message: "请选择批次", trigger: "blur" }], appId: [{ required: true, message: "请选择应用", trigger: "blur" }] }
    }
  },
  created() {
    this.getTabularData()
    this.getAppName()
    this.getBatchTwos()
  },
  methods: {
    getTabularData() {
      timequeryAppControl(this.listQuery).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        this.tableData = []
        this.total = 0
      })
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), id: this.deleteDialogs.deleteId }
      deleteAppControl(params).then(res => {
        this.$message.success(res.msg)
        this.getTabularData()
      })
    },
    //根据ID查询霸屏
    getqueryIdAppControl(id) {
      this.editSubjects = false
      this.subjectId = id
      queryIdAppControl({ id: id }).then(res => {
        this.dialogFormVisible = true
        this.addForm.batch = res.data.batch.split(',')
        this.addForm.appId = res.data.app_id
      }).catch(err => { })
    },
    //修改霸屏
    gettimeeditAppControl(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.batch = this.addForm.batch.toString()
          addForm.id = this.subjectId
          timeeditAppControl(addForm).then(res => {
            this.dialogFormVisible = false
            this.$message.success(res.msg)
            this.getTabularData()
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    //增加应用霸屏
    getaddAppControl(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.batch = this.addForm.batch.toString()
          addAppControl(addForm).then(res => {
            this.$message.success(res.msg)
            this.getTabularData()
            this.dialogFormVisible = false
          }).catch(err => {})
        } else {
          return false
        }
      })
    },
    //添加按钮事件
    btnaddBrand() {
      this.editSubjects = true
      this.dialogFormVisible = true
    },
    //根据ID获取批次列表
    getBatchTwos() {
      let data = { userId: localStorage.getItem("ID") }
      getBatchTwo(data).then(res => {
        this.lotoptions = res.data
      }).catch(err => {
        this.lotoptions = []
      })
    },
    //根据ID获取应用
    getAppName() {
      let data = { userId: localStorage.getItem("ID") }
      getAppName(data).then(res => {
        this.appOptions = res.data
      }).catch(err => {
        this.appOptions = []
      })
    },
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = { userId: localStorage.getItem("ID"), last_ip: sessionStorage.getItem("Ip"), batch: "", appId: "" }
      })
    }
  }
}
</script>
